package com.raidroid.rdmute;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.widget.RemoteViews;

public class RDMuteWidget extends AppWidgetProvider{

	public static final String URI_SCHEME = "rdmute_widget";
	private static Handler handler = new Handler();
	
	@Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
    }
	
	@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		context.startService(new Intent(context, UpdateService.class));	
    }
	
	public static class UpdateService extends Service {

		@Override
        public void onStart(Intent intent, int startId) {
            handler.removeCallbacks(mUpdateWidgetTask);
            handler.postDelayed(mUpdateWidgetTask, 20000);
        } 
		
		private Runnable mUpdateWidgetTask = new Runnable(){
			public void run(){
				AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(UpdateService.this);
	            RemoteViews remoteView = new RemoteViews(UpdateService.this.getPackageName(), R.layout.rdmutewidget);
	            ComponentName appWidgetId = new ComponentName(UpdateService.this, RDMuteWidget.class);
	            
	            if(RDMuteService.IsServiceRunning){
	            	Intent defineIntent = new Intent("com.raidroid.rdmute.CLICK");
	                PendingIntent pendingIntent = PendingIntent.getBroadcast(UpdateService.this, 0, defineIntent, 0); 
	                remoteView.setImageViewResource(R.id.RdMuteOnOffButton, R.drawable.widgetgreen_1);
	                remoteView.setOnClickPendingIntent(R.id.RdMuteOnOffButton, pendingIntent);
	                appWidgetManager.updateAppWidget(appWidgetId, remoteView);                
	            }else{
	            	Intent defineIntent = new Intent("com.raidroid.rdmute.CLICK");
	                PendingIntent pendingIntent = PendingIntent.getBroadcast(UpdateService.this, 0, defineIntent, 0); 
	                remoteView.setImageViewResource(R.id.RdMuteOnOffButton, R.drawable.widgetred_1);
	                remoteView.setOnClickPendingIntent(R.id.RdMuteOnOffButton, pendingIntent);
	                appWidgetManager.updateAppWidget(appWidgetId, remoteView);
	            }
	            handler.postDelayed(this, 20000);
			}
		};
		
		@Override
		public IBinder onBind(Intent arg0) {
			// TODO Auto-generated method stub
			return null;
		}
		
	}
	
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);        
        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
        	
        }        
    }
   
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            final int appWidgetId = intent.getExtras().getInt(
            		AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
            if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                this.onDeleted(context, new int[] { appWidgetId });
            }
        }else if(action.equals("com.raidroid.rdmute.CLICK")){     
        	if(RDMuteService.IsServiceRunning){
        		RDMuteService.IsServiceRunning = false;
        		context.stopService(new Intent(context, RDMuteService.class));        		
        		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        		ComponentName appWidgetId = new ComponentName(context, RDMuteWidget.class);
                RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.rdmutewidget);
                remoteView.setImageViewResource(R.id.RdMuteOnOffButton, R.drawable.widgetred_1);
                appWidgetManager.updateAppWidget(appWidgetId, remoteView);
                
        	}else{
        		RDMuteService.IsServiceRunning = true;
        		context.startService(new Intent(context, RDMuteService.class));        		
        		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        		ComponentName appWidgetId = new ComponentName(context, RDMuteWidget.class);
        		RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.rdmutewidget);
                remoteView.setImageViewResource(R.id.RdMuteOnOffButton, R.drawable.widgetgreen_1);
                appWidgetManager.updateAppWidget(appWidgetId, remoteView);
        	}
        	super.onReceive(context, intent);
        } else {
            super.onReceive(context, intent);
        }
    }
}
